from django.shortcuts import render
from django.views.decorators.http import require_http_methods
from django.core import serializers
from django.http import JsonResponse

from backend.api.login import userSignup, userLogin
from backend.api.getbook import getBooklist, loadBook
from backend.api.exercise import addExercise, getUnCheckedExercise, checkExercise, loadExercise
from backend.api.operatePlan import addPlan, updatePlan, loadPlan, getPlanList, loadReview, getPlanInfo
from backend.api.operateNote import addNote, updateNote, getNoteByUser, getNoteByWord, getUnCheckedNote, checkNote
from backend.api.synclearning import syncLearning
from backend.api.checkInRecord import checkInRecord
# Create your views here.

# 用户注册
@require_http_methods(["POST"])
def signup(request):
    response = {}
    try:
        username = request.POST.get('username')
        pwd = request.POST.get('pwd')
        response = userSignup(username, pwd)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 用户登录
@require_http_methods(["GET"])
def login(request):
    response = {}
    try:
        username = request.GET.get('username')
        pwd = request.GET.get('pwd')
        response = userLogin(username, pwd)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 获取词书列表
@require_http_methods(["GET"])
def getbooklist(request):
    response = {}
    try:
        response = getBooklist()
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 下载词书
@require_http_methods(["GET"])
def loadbook(request):
    response = {}
    try:
        bid = request.GET.get('bid')
        response = loadBook(bid)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 新增题目
@require_http_methods(["POST"])
def addexercise(request):
    response = {}
    try:
        word = request.POST.get('word')
        question = request.POST.get('question')
        choice_A = request.POST.get('choice_A', '')
        choice_B = request.POST.get('choice_B', '')
        choice_C = request.POST.get('choice_C', '')
        choice_D = request.POST.get('choice_D', '')
        answer = request.POST.get('answer')
        creator = request.POST.get('creator')

        response = addExercise(word, question, choice_A,
                               choice_B, choice_C, choice_D, answer, creator)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 新增计划
@require_http_methods(["GET"])
def addplan(request):
    response = {}
    try:
        uid = request.GET.get('uid')
        bid = request.GET.get('bid')
        everyday_num = request.GET.get('everyday_num')
        response = addPlan(uid, bid, everyday_num)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 修改计划
@require_http_methods(["GET"])
def updateplan(request):
    response = {}
    try:
        pid = request.GET.get('pid')
        everyday_num = request.GET.get('everyday_num')
        response = updatePlan(pid, everyday_num)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 获取计划列表
@require_http_methods(["GET"])
def getplanlist(request):
    response = {}
    try:
        uid = request.GET.get('uid')
        response = getPlanList(uid)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 加载学习计划
@require_http_methods(["GET"])
def loadplan(request):
    response = {}
    try:
        pid = request.GET.get('pid')
        response = loadPlan(pid)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)


# 新增笔记
@require_http_methods(["POST"])
def addnote(request):
    response = {}
    try:
        word = request.POST.get('word')
        content = request.POST.get('content')
        creator = request.POST.get('creator')
        response = addNote(word, content, creator)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 修改笔记
@require_http_methods(["POST"])
def updatenote(request):
    response = {}
    try:
        content = request.POST.get('content')
        nid = request.POST.get('nid')
        response = updateNote(nid, content)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 按用户筛选笔记
@require_http_methods(["GET"])
def getnotebyuser(request):
    response = {}
    try:
        uid = request.GET.get('uid')
        response = getNoteByUser(uid)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 按单词筛选笔记
@require_http_methods(["GET"])
def getnotebyword(request):
    response = {}
    try:
        word = request.GET.get('word')
        response = getNoteByWord(word)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 同步学习计划
@require_http_methods(["GET"])
def synclearning(request):
    response = {}
    try:
        pid = request.GET.get('pid')
        word = request.GET.get('word')
        ignore = request.GET.get('ignore')
        response = syncLearning(pid, word, ignore)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 获取未审核笔记
@require_http_methods(["GET"])
def getuncheckednote(request):
    response = {}
    try:
        response = getUnCheckedNote()
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 获取未审核习题
@require_http_methods(["GET"])
def getuncheckedexercise(request):
    response = {}
    try:
        response = getUnCheckedExercise()
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 获取习题
@require_http_methods(["GET"])
def loadexercise(request):
    response = {}
    try:
        pid = request.GET.get('pid')
        response = loadExercise(pid)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 审核笔记
@require_http_methods(["GET"])
def checknote(request):
    response = {}
    try:
        nid = request.GET.get('nid')
        approve = request.GET.get('approve')
        response = checkNote(nid, approve)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 审核习题
@require_http_methods(["GET"])
def checkexercise(request):
    response = {}
    try:
        exid = request.GET.get('exid')
        approve = request.GET.get('approve')
        response = checkExercise(exid, approve)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 打卡记录
@require_http_methods(["GET"])
def checkinrecord(request):
    response = {}
    try:
        pid = request.GET.get('pid')
        response = checkInRecord(pid)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 加载学习计划
@require_http_methods(["GET"])
def loadreview(request):
    response = {}
    try:
        pid = request.GET.get('pid')
        response = loadReview(pid)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)

# 获取计划详细内容
@require_http_methods(["GET"])
def getplaninfo(request):
    response = {}
    try:
        pid = request.GET.get('pid')
        response = getPlanInfo(pid)
    except Exception as e:
        response['msg'] = str(e)
        response['ok'] = 0
    return JsonResponse(response)